package com.liu.voluntary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.voluntary.mapper.UserMapper;
import com.liu.voluntary.pojo.Admin;
import com.liu.voluntary.pojo.User;
import com.liu.voluntary.service.UserService;
import com.liu.voluntary.util.CookieUtil;
import com.liu.voluntary.util.MD5Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/userList")
    public String UserList(Model model,@RequestParam(defaultValue = "1") int currentPage){
        int pageSize = 5;

        Page page = new Page<>(currentPage,pageSize);

        userMapper.selectPage(page, null);
        List user = page.getRecords();
        List<User> userList = userMapper.selectList(null);
        int number = userList.size();
        int totalPages = (int) Math.ceil((double) number / pageSize);

        model.addAttribute("totalPages",totalPages);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("users",user);

        return "userList";
    }

    @RequestMapping("/toAddPage")
    public String toAddPage(Model model){

        return "userRegister";
    }



    @RequestMapping("/deleteUser/{id}")
    public String toDelete(Model model, @PathVariable("id") String id){

        int i = userMapper.deleteById(id);
        if (i != 0){
            model.addAttribute("msg","删除成功");
        }

        return "redirect:/userList";

    }



    @RequestMapping("/updateUserPage/{id}")
    public String toUpdatePage(Model model, @PathVariable("id") String id){

        User user = userMapper.selectById(id);
        model.addAttribute("user",user);

        return "userUpdate";

    }


    @RequestMapping("/updateUser/{id}")
    public String toUpdate(@RequestParam("nickname") String nickname,
                           @RequestParam("password") String password,
                           @PathVariable("id") String id,
                           Model model){

        User user = new User();
        user.setNickname(nickname);

        String dbPass = MD5Util.inputPassToDBPass(password);
        user.setPassword(dbPass);

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();

        wrapper.eq("id",id);

        int i = userMapper.update(user, wrapper);



        if (i == 0){
            model.addAttribute("msg","更新失败");
        }

        return "redirect:/userList";

    }



    //修改密码
    @RequestMapping("/updateUserPwdPage")
    public String updatePwdPage(HttpServletRequest request,Model model){

        return "updateUserPwdPage";

    }



    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate redisTemplate;
    // 处理修改密码请求
    @PostMapping("/updateUserPassword")
    public String updateUserPassword(@CookieValue("Ticket") String Ticket,
                                     @RequestParam(value = "oldPassword") String oldPassword,
                                     @RequestParam(value = "newPassword") String newPassword,
                                     Model model, HttpServletRequest request) {
        // 获取当前登录用户
        User user =(User) redisTemplate.opsForValue().get("user:" + Ticket);

        if (user == null) {
            return "redirect:/toLoginPage"; // 如果没有登录则重定向到登录页面
        }

        // 验证旧密码
        if (!user.getPassword().equals(MD5Util.inputPassToDBPass(oldPassword))){
           return "redirect:/updateUserPwdPage?error=true";
       }

        // 更新密码
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
       updateWrapper.set("password",MD5Util.inputPassToDBPass(newPassword))
                    .eq("mobile",user.getMobile());
        userService.update(updateWrapper);




        // 设置成功消息
        model.addAttribute("success", true);

        return "redirect:/updateUserPwdPage?success=true";
    }



    @RequestMapping("/notify")
    public String notifyUser(HttpServletRequest request,Model model){


        String ticket = CookieUtil.getCookieValue(request, "Ticket");
        User user =(User) redisTemplate.opsForValue().get("user:"+ticket);

        String mobile = user.getMobile();

        model.addAttribute("mobile",mobile);

        return "notify";

    }




    

    @RequestMapping("/info")
    public String userInfo(@CookieValue("Ticket") String Ticket,Model model) {

        User user = (User) redisTemplate.opsForValue().get("user:" + Ticket);
        
        model.addAttribute("user", user);
        return "userInfo";
    
}

    @RequestMapping("/user")
    public String userPage(@CookieValue("Ticket") String Ticket, Model model) {
        User user = (User) redisTemplate.opsForValue().get("user:" + Ticket);
        if (user != null) {
            model.addAttribute("mobile", user.getMobile());
        }
        return "user";
    }

}
